package com.gopay.bis.cmbc.ebank.operation;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import Union.JnkyServer;
import Union.UnionException;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.common.util.TransUtils;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.ICertHelper;

/**
 * 民生银行B2C签名/验签类
 * 
 * @CerthelperCMBCB2C.java
 * @author zhengxue
 */
public class CmbcB2cCerthelper implements ICertHelper {
	/** cer证书路径. */
	private String cerFilePath = null;
	/** pfx证书路径. */
	private String pfxFilePath = null;
	/** 证书密码. */
	private String certPswd = null;
	/** 充值功能pfx证书路径. */
	private String rechargePfxFilePath = null;
	/** 充值功能证书密码. */
	private String rechargePswd = null;
	
	private static Log logger = LogFactory.getLog(CmbcB2cCerthelper.class);

	public String sign(BaseMessage bsaeMessage) throws GopayException {
		try {
			String sginStr = "";
			BankLogUtils.printB2CSignPlainLog(bsaeMessage);
			if (!bsaeMessage.isSupportCredit()) {
				// 充值
				sginStr = rechargeSign(bsaeMessage);
			} else {
				sginStr = paySign(bsaeMessage);
			}
			BankLogUtils.printB2CSignSignatureLog(bsaeMessage, sginStr);
			return sginStr;
		 } catch (Exception e) {
			BankLogUtils.printB2CSignExceptionLog(bsaeMessage, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6029);
		}
	}

	/**
	 * 民生B2C支付交易签名
	 * 
	 * @param Object
	 *            plainText
	 * @return Object
	 * @throws Exception
	 * @throws UnionException
	 */
	public String paySign(BaseMessage baseMessage) throws Exception {
		String sginStr = getJnkyServer(baseMessage).EnvelopData((String) baseMessage.getPlainMessage());
		return sginStr;

	}

	/**
	 * 民生B2C充值交易签名
	 * 
	 * @param plainText
	 * @return
	 * @throws Exception
	 * @throws UnionException
	 */
	public String rechargeSign(BaseMessage baseMessage) throws Exception {

		String sginStr = getRechargeJnkyServer(baseMessage).EnvelopData((String) baseMessage.getPlainMessage());
		return sginStr;
	}

	/**
	 * 民生验签
	 * 
	 * @param encText
	 * @return
	 * @throws GopayException 
	 * 
	 */
	public Object verify(BaseMessage baseMessage) throws GopayException {
		String isVeriyPass = "";
		try {
			isVeriyPass = getRechargeJnkyServer(baseMessage).DecryptData((String) baseMessage.getSignMessage());
			BankLogUtils.printB2CVerifyPlainLog(baseMessage);
			BankLogUtils.printB2CVerifySignatureLog(baseMessage, baseMessage.getSignMessage());
			BankLogUtils.printB2CVerifyResultLog(baseMessage, !isVeriyPass.equals(""));
			return isVeriyPass;
		} catch (UnionException e) {
			try {
				//因证书使用错误导致CMBC验签失败，可再次进行验签
				isVeriyPass = getJnkyServer(baseMessage).DecryptData((String) baseMessage.getSignMessage());
				BankLogUtils.printB2CVerifyPlainLog(baseMessage);
				BankLogUtils.printB2CVerifySignatureLog(baseMessage, baseMessage.getSignMessage());
				BankLogUtils.printB2CVerifyResultLog(baseMessage, !isVeriyPass.equals(""));
				return isVeriyPass;
			} catch (Exception ex) {
				BankLogUtils.printB2CSignExceptionLog(baseMessage, e);
				throw new GopayException(ProcCodeConstants.PROC_CODE_100E6031);
			}
		}  catch (Exception e) {
			BankLogUtils.printB2CSignExceptionLog(baseMessage, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6031);
		}
	}

	public void setCerFilePath(String cerFilePath) {
		this.cerFilePath = cerFilePath;
	}

	public void setPfxFilePath(String pfxFilePath) {
		this.pfxFilePath = pfxFilePath;
	}

	public void setCertPswd(String certPswd) {
		this.certPswd = certPswd;
	}

	public void setRechargePfxFilePath(String rechargePfxFilePath) {
		this.rechargePfxFilePath = rechargePfxFilePath;
	}

	public void setRechargePswd(String rechargePswd) {
		this.rechargePswd = rechargePswd;
	}

	public JnkyServer getJnkyServer(BaseMessage baseMessage) throws Exception {
		byte[] cerFile = null;
		byte[] pfxFile = null;
		String pswd = null;
		pfxFile = TransUtils.readCertFileForCMBCB2C(baseMessage.getPrivateKeyPath());
		pswd = baseMessage.getPrivatePwd();
		cerFile = TransUtils.readCertFileForCMBCB2C(baseMessage.getPublicKeyPath());
		JnkyServer jnkyServer = new JnkyServer(cerFile, pfxFile, pswd);
		return jnkyServer;
	}

	public JnkyServer getRechargeJnkyServer(BaseMessage baseMessage) throws Exception {
		byte[] cerFile = null;
		byte[] pfxFile = null;
		String pswd = null;
		pfxFile = TransUtils.readCertFileForCMBCB2C(baseMessage.getPrivateKeyPath2nd());
		pswd = baseMessage.getPrivatePwd2nd();
		cerFile = TransUtils.readCertFileForCMBCB2C(baseMessage.getPublicKeyPath());
		JnkyServer rechargeJnkyServer = new JnkyServer(cerFile, pfxFile, pswd);
		return rechargeJnkyServer;
	}
}